#!/usr/bin/env perl

# use strict;
use warnings;

BEGIN {
	push @INC,$ENV{CODEMGR_LIBS} if exists($ENV{CODEMGR_LIBS});
}


use Getopt::Long;
use File::Basename;
use File::Find;
use File::Spec;
use Config;
use Cwd;
use codemgr2 qw(logmsg warnmsg errormsg parse_rlogs load_config globfiles
		get_project attempt_alias_map break_into_dirs);
eval "use pv3;";
if($@) {
	errormsg("Unable to open PV3 utility library.",1);
}

$ARG_project=undef;

sub usage {
	$_PRG=basename($0);
	$_SPC=" " x length($_PRG);
	print "
Usage: $_PRG [--project P]

The $_PRG utility takes the current project, or the one specified
and dumps the SQL data in a portable format for import elsewhere.
This utility is only suitable for PV3-based backend repositories.

Written by Simon Edwards (C) 2009-2010, advantsys.co.uk
For more information see the $_PRG(1) manual page.
";
	exit(1);
}

if(!exists($ENV{PROJECTS_ROOT})) {
	errormsg("Required environment variable \"PROJECTS_ROOT\" is not set.",3);
}

if(! -d $ENV{PROJECTS_ROOT}) {
	errormsg("\"PROJECTS_ROOT\" directory \"$PROJECTS_ROOT\" does not exist.",2);
}
$PROJECTS_ROOT=$ENV{PROJECTS_ROOT};


if(!GetOptions(
	"project=s"	=> \$ARG_project,
)) {
	usage();
}

if(!defined($ARG_project)) {
	if(!exists($ENV{CM2_REALPATH}) || $ENV{CM2_REALPATH}==0) {
		@r=get_project(getcwd,undef);
	} else {
		@r=get_project($ENV{PWD},undef);
	}
	if(defined($r[0])) {
		$ARG_project=$r[0];
	} else {
		if($r[1]==1) {
			errormsg("No project owning this directory has been found!",1);
		}
		if($r[1]==2) {
			errormsg("Multiple projects owning this directory have been found!",1);
		}
		errormsg($r[2],2);
		exit 1;
	}
} else {
	#################################################################
	# Work out the project based on ARG_project when might simlpy	#
	# be an alias rather than the real project name...		#
	#################################################################
	if(! -d "$PROJECTS_ROOT/$ARG_project") {
		$alias=$ARG_project;
		$realname=attempt_alias_map($ARG_project);
		if(defined($realname)) {
			$ARG_project=$realname;
			logmsg("Matched project \"$realname\" via alias \"$alias\".");
		} else {
			errormsg("Project source directory \"$ARG_project\" does not exist.",2);
		}
	}
}

my $config=load_config("$PROJECTS_ROOT/$ARG_project/.meta");
if(exists($config->{REPOS_TYPE})) {
	$repos_type=$config->{REPOS_TYPE}->[0];
} else {
	errormsg("Missing REPOS_TYPE definition for repository!",1);
}
if($repos_type!=3) {
	errormsg("The project in question is using a V$repos_type repository rather than a V3 one.",1);
}

$errs=0;
print "%%PROJECT_NAME%% $ARG_project\n";
for my $cd (@{$config->{SOURCE_DIR}}) {
	next if ! -f "$PROJECTS_ROOT/$ARG_project/$cd/PV3,pv3";
	if(! -r _) {
		errormsg("Unable to read '$PROJECTS_ROOT/$ARG_project/$cd/PV3,pv3' - ignoring.");
		$errs++;
		next;
	}
	my $r=pv3::dump_database(PVFILE => "$PROJECTS_ROOT/$ARG_project/$cd/PV3,pv3");
	if(! defined($r->[0])) {
		errormsg($r->[1]);
		$errs++;
		next;
	}
	print "%%DATA_SOURCE_DIR%% $cd\n";
	print "$_\n" foreach(@{$r->[0]});
}
exit(0) if !$errs;
exit(1);
